//
//  JSQAudioMediaItem.m
//  Pods
//
//  Created by C_HAO on 15/4/20.
//
//

#import "JSQAudioMediaItem.h"
#import "JSQMessagesMediaViewBubbleImageMasker.h"
#import "UIImage+JSQMessages.h"
#import "UIColor+JSQMessages.h"

@interface JSQAudioMediaItem ()

@property (strong, nonatomic) UIImageView *cachedAudioMediaView;

@end

@implementation JSQAudioMediaItem

#pragma mark - Initialization

- (instancetype)initWithFileURL:(NSURL *)fileURL complete:(void (^)(void))complete {
    self = [super init];
    if (self) {
        _fileURL = [fileURL copy];
        _complete = complete;
    }
    return self;
}

- (void)dealloc {
    _fileURL = nil;
}

#pragma mark - Setters

- (void)setFileURL:(NSURL *)fileURL {
    _fileURL = [fileURL copy];
}

- (void)setAppliesMediaViewMaskAsOutgoing:(BOOL)appliesMediaViewMaskAsOutgoing {
    [super setAppliesMediaViewMaskAsOutgoing:appliesMediaViewMaskAsOutgoing];
}

#pragma mark - JSQMessageMediaData protocol

- (UIView *)mediaView {
    if (self.fileURL == nil) {
        return nil;
    }

    if (self.cachedAudioMediaView == nil) {
        UIImage *image;
        if (self.appliesMediaViewMaskAsOutgoing) {
            image = [UIImage jsq_defaultLeftAudioImage];
        } else {
            image = [UIImage jsq_defaultRightAudioImage];
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        CGSize size = [self mediaViewDisplaySize];
        imageView.contentMode = UIViewContentModeCenter;
        imageView.clipsToBounds = YES;
        imageView.frame = CGRectMake(0, 0, size.width, size.height);
        [JSQMessagesMediaViewBubbleImageMasker applyBubbleImageMaskToMediaView:imageView isOutgoing:self.appliesMediaViewMaskAsOutgoing];
        self.cachedAudioMediaView = imageView;
    }

    return self.cachedAudioMediaView;
}

- (CGSize)mediaViewDisplaySize {
    return CGSizeMake(120, 50);
}

- (NSUInteger)mediaHash {
    return self.hash;
}

#pragma mark - NSObject

- (BOOL)isEqual:(id)object {
    if (![super isEqual:object]) {
        return NO;
    }

    JSQAudioMediaItem *videoItem = (JSQAudioMediaItem *)object;

    return [self.fileURL isEqual:videoItem.fileURL];
}

- (NSUInteger)hash {
    return super.hash ^ self.fileURL.hash;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: fileURL=%@, appliesMediaViewMaskAsOutgoing=%@>",
            [self class], self.fileURL, @(self.appliesMediaViewMaskAsOutgoing)];
}

#pragma mark - NSCoding

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        _fileURL = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(fileURL))];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:self.fileURL forKey:NSStringFromSelector(@selector(fileURL))];
}

#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone {
    JSQAudioMediaItem *copy = [[[self class] allocWithZone:zone] initWithFileURL:self.fileURL
                                                                        complete:_complete];
    copy.appliesMediaViewMaskAsOutgoing = self.appliesMediaViewMaskAsOutgoing;
    return copy;
}

@end
